﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLNhaThuoc.BL;
using QLNhaThuoc.DTO;
using NCalc;

namespace QLNhaThuoc
{
    public partial class From_Mapping : Form
    {
        MappingThuocControl mappingThuocBL = new MappingThuocControl();
        MappingNhanVienControl mappingNhanVienBL = new MappingNhanVienControl();

        private string expression = string.Empty;
        List<ExpressionDTO> dataExpression = new List<ExpressionDTO>();
        BindingSource bs = new BindingSource();
        List<string> parameters = new List<string>();

        private int rowSelected = -1;

        public From_Mapping()
        {
            InitializeComponent();
            cbb_maLoaiFile.SelectedIndex = 0;
        }

        private void btn_getPath_Click(object sender, EventArgs e)
        {
            if(openFileXML.ShowDialog() == DialogResult.OK)
            {
                string tenFile = openFileXML.FileName;
                tb_duongDanFile.Text = openFileXML.FileName;

                if (cbb_maLoaiFile.Text.CompareTo("Thuốc") == 0)
                {
                    mappingThuocBL.layDuLieuTuXML(tenFile);
                    cbb_thuocTinhXML.DataSource = mappingThuocBL.layThuocTinh();
                }
                else
                {
                    mappingNhanVienBL.layDuLieuTuXML(tenFile);
                    cbb_thuocTinhXML.DataSource = mappingNhanVienBL.layThuocTinh();
                }
            }            
        }

        private void cbb_maLoaiFile_SelectedValueChanged(object sender, EventArgs e)
        {
            string maLoaiFile = cbb_maLoaiFile.Text;
            //string[] thuocARR = new string[11]{"MaThuoc", "Ten", "MaLoai", "DonGia", "DonVi", "MaCongTy", "NgaySanXuat", "HanSuDung", "GhiChu", "TonToiThieu", "TheoChiDinh"};
            //string[] nhanVienARR = new string[11]{"MaNV", "Ho", "Ten", "LoaiNV", "GioiTinh", "NgaySinh", "DiaChi", "SDT", "NgayVaoLam", "Username", "Password"};

            NhanVienControl nvCtrl = new NhanVienControl();
            string[] nhanVienARR = nvCtrl.GetAllColumns();

            ThuocControl thuocCtrl = new ThuocControl();
            string[] thuocARR = thuocCtrl.GetAllColumns();

            cbb_thuocTinhDB.DataSource = maLoaiFile.CompareTo("Thuốc") == 0 ? thuocARR : nhanVienARR;
        }

        private void btn_cong_Click(object sender, EventArgs e)
        {
            if (expression.CompareTo(string.Empty) == 0)
            {
                expression = "[" + cbb_thuocTinhXML.Text + "]";
            }
            else
            {
                expression = expression + " + " + "\' \'"  + " + " + "[" + cbb_thuocTinhXML.Text + "]";
            }

            parameters.Add(cbb_thuocTinhXML.Text);
            tb_expression.Text = expression;
        }

        private void cbb_thuocTinhDB_SelectedValueChanged(object sender, EventArgs e)
        {
            expression = string.Empty;
            parameters.Clear();
            tb_expression.Text = string.Empty;
        }

        private void btn_them_Click(object sender, EventArgs e)
        {
            ExpressionDTO exp = new ExpressionDTO();

            string valueDB = cbb_thuocTinhDB.Text;
            
            if(!kiemTraDuLieu(valueDB))
            {
                exp.Name = valueDB;
                exp.Expression = expression;

                foreach (string parameter in parameters)
                {
                    exp.Parameters.Add(parameter);
                }

                dataExpression.Add(exp);
                parameters.Clear();
                hienThiList();
            }else
            {
                MessageBox.Show("Trường " + cbb_thuocTinhDB.Text + " này đã tồn tại!");
            }
        }

        private bool kiemTraDuLieu(string value)
        {
            return dataExpression.Any(exp => exp.Name.CompareTo(value) == 0);
        }

        private void hienThiList()
        {
            bs.DataSource = dataExpression;
            dgv_results.DataSource = null;
            dgv_results.AutoGenerateColumns = false;
            dgv_results.DataSource = bs;
        }
        private void btn_xoa_Click(object sender, EventArgs e)
        {
            dataExpression.RemoveAt(rowSelected);
            parameters.Clear();
            hienThiList();
        }

        private void dgv_results_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            rowSelected = e.RowIndex;

            cbb_thuocTinhDB.Text = string.Empty;
            cbb_thuocTinhDB.SelectedText = dataExpression[rowSelected].Name;
            tb_expression.Text = dataExpression[rowSelected].Expression;
            parameters = dataExpression[rowSelected].Parameters;
        }

        private void btn_capnhat_Click(object sender, EventArgs e)
        {
            dataExpression[rowSelected].Name = cbb_thuocTinhDB.Text;
            dataExpression[rowSelected].Expression = tb_expression.Text;
            dataExpression[rowSelected].Parameters = parameters;

            hienThiList();
        }

        private void btn_clear_Click(object sender, EventArgs e)
        {
            expression = string.Empty;
            tb_expression.Text = expression;
            parameters.Clear();
        }

        private void btn_mapping_Click(object sender, EventArgs e)
        {
            string result = cbb_maLoaiFile.Text.CompareTo("Thuốc") == 0 ? mappingThuocBL.mappingThuoc(dataExpression) : mappingNhanVienBL.mappingNhanVien(dataExpression);

            MessageBox.Show(result);
        }
    }
}
